#version 150

in vec2 fragTexCoord;
in vec3 fragPosition;
in vec3 fragLightPos;
in vec3 fragNormal;
in vec3 fragEyeDir;

uniform sampler2D Texture0;
uniform sampler2D Texture1;

uniform vec3 Color0 = vec3(1, 1, 1);

void CalculateLight(in vec3 InNormal, in vec3 Position, in vec3 LightPosition, in vec3 LightColor, in vec3 MaterialColor, in vec3 EyeDir, out vec3 Color)
{
    vec3 Normal = normalize( texture2D(Texture1, fragTexCoord).xyz * 2.0 - 1.0);

    vec3 AmbientColor = vec3(0.05, 0.05, 0.05);

    vec3 L = normalize(LightPosition - Position);
    float diffuseLight = max(dot(Normal, L), 0);
    vec3 DiffuseColor = MaterialColor * LightColor * diffuseLight;

    float AttA = 0.7;
    float AttB = 0.01;
    float AttC = 0.004; //0.004 - nice effect

    float d = distance(Position, LightPosition);

    float Attenuation = 1/(AttA + AttB * d + AttC * d * d);

    int shininess = 15;

    vec3 E = normalize(EyeDir);
    vec3 R = reflect(-L, Normal);
    float spec = pow(max(dot(R, E), 0.0), shininess);

    Color = AmbientColor + Attenuation * DiffuseColor;
    if(shininess >= 0) Color += spec * MaterialColor;
}

void main()
{
    vec4 finalCol = texture2D(Texture0, fragTexCoord);
    vec3 lightCol = vec3(1, 1, 1);
    CalculateLight(fragNormal, fragPosition, fragLightPos, lightCol, lightCol, fragEyeDir, lightCol);

    gl_FragColor = finalCol*vec4(lightCol, 1);
}



























































